package aaa;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;

import com.attilax.json.AtiJson;
import com.attilax.net.urlEncode;
import com.google.common.collect.Lists;

/**
 * 
 * 
 * Domain
 * 
 * ----------------------------------------------------------------------------
 * --- WORKGROUP ��������� ע��
 * 
 * ----------------------------------------------------------------------------
 * --- \\AT6T5TKUVHGT0IG java.lang.StringIndexOutOfBoundsException: String index
 * out of range: -2 \\DY java.lang.StringIndexOutOfBoundsException: String index
 * out of range: -2 \\GLEJSAZRY5YIIW0 java.lang.StringIndexOutOfBoundsException:
 * String index out of range: -2 \\USER-20151223FC
 * java.lang.StringIndexOutOfBoundsException: String index out of range: -2
 * \\USER-20151225DJ java.lang.StringIndexOutOfBoundsException: String index out
 * of range: -2 ����ɹ���ɡ�
 * 
 * java.lang.StringIndexOutOfBoundsException: String index out of range: -2
 * ����ɹ���ɡ�
 * 
 * WORKGROUP AT6T5TKUVHGT0IG ���� 192.168.2.99 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128 DY
 * ���� 192.168.2.105 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=64 GLEJSAZRY5YIIW0 ����
 * 192.168.2.107 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128 USER-20151223FC ���� 192.168.2.100
 * �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128 USER-20151225DJ ���� 192.168.2.102 �Ļظ�: �ֽ�=32
 * ʱ��<1ms TTL=128 ��ɹ���ɡ� Ping �����Ҳ������� ??????��?���������ƣ�Ȼ�����ԡ�
 * ����ɹ���ɡ�
 * 
 * 
 * 
 * net view /domain:WORKGROUP
 * 
 * @author Administrator
 *
 */
public class Neighborhood {

	private static String getWorkgroup() {
		String line = "";
		boolean begin = false;
		StringBuffer sb = new StringBuffer(); // 可变长
		try {
			String command = null;
			Process process = Runtime.getRuntime().exec(command); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				line = line.trim();

				// 你的工作组千万不要命名为“命令成功完成。”或者是“The command completed successfully.”
				if (line.endsWith("命令成功完成。")
						|| line.equalsIgnoreCase("The command completed successfully.")) {
					begin = false;
					break;
				}
				if (begin) {
					String domain = null;
					if (domain.length() > 0) {
						if (line.indexOf(" ") != -1)
							line = line.substring(2, line.indexOf(" "));
						else
							line = line.substring(2, line.length());

						// line = line + " " + getNeighborIP(line);
						sb.append("  ").append(line).append("\r\n");

						// System.out.println( "得到   "+line );
					} else {
						sb.append(line).append("\r\n");
						// System.out.println( "得到 "+line );
						// getNeighborhood( "net view /domain:"+line,"");
						// sb.append( getNeighborhood(
						// "net view /domain:"+line,line));
					}
				}
				if (line.endsWith("-")) {
					begin = true;
				}

			}
			br.close();
			process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。
		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
		return sb.toString();
	}

	private static List<String> getNeighborhood() {
		List<String> li = Lists.newArrayList();
		String line = "";
		boolean begin = false;
		String command = "net view";
		StringBuffer sb = new StringBuffer(); // 可变长
		try {
			
			Process process = Runtime.getRuntime().exec(command); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream(),"gbk"));
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				line = line.trim();

				// 你的工作组千万不要命名为“命令成功完成。”或者是“The command completed successfully.”
				if (line.endsWith("命令成功完成。")
						|| line.equalsIgnoreCase("The command completed successfully.")) {
					// begin=false;
					break;
				}
				if (line.endsWith("--------")) {
					begin = true;
					// next()
					continue;
				}
				if (begin) {

					{
						if (line.indexOf(" ") != -1)  //if line contain empty char
							line = line.substring(2, line.indexOf(" "));
						else
							line = line.substring(2, line.length());

						li.add(line);
						// line = line + " " + getNeighborIP(line);
						//sb.append("  ").append(line).append("\r\n");

						// System.out.println( "得到   "+line );

					}
				

				}
			}
				br.close();
				process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。
			
		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
		//String string = sb.toString();
	//	li.add(string);
		return li;
	}

	public static String getNeighborIP(String user) {

		String userIP = "";

		try {

			Process process = Runtime.getRuntime().exec("Ping " + user + " -4"); // 该Process实例可用来控制进程并获得相关信息
			// 每个 Java 应用程序都有一个 Runtime 类实例，使应用程序能够与其运行的环境相连接。
			// 可以通过 getRuntime 方法获取当前运行时。exec(String)在单独的进程中执行指定的字符串命令
			BufferedReader br = new BufferedReader(new InputStreamReader(
					process.getInputStream()));

			userIP = br.readLine();
			if (userIP.length() == 0) { // 可以连接
				userIP = br.readLine();
				userIP = br.readLine();

				userIP = userIP.substring(userIP.indexOf("[") + 1,
						userIP.indexOf("]") - 1);
				System.out.println(userIP);
			}
			// else//不可以连接
			// System.out.println(userIP);s
			br.close();
			process.waitFor(); // 导致当前线程等待，如有必要，一直要等到由该 Process 对象表示的进程已经终止。

		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}

		return userIP;

	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		
		System.out.println(com.attilax.net.URLEncoder.encodeDouble("uname='attilax'" ));
		System.out.println(System.getProperty("file.encoding"));
		System.setProperty("file.encoding","gbk");
		System.out.println(System.getProperty("file.encoding"));
		List<String> li = Neighborhood.getNeighborhood( );
		System.out.println(AtiJson.toJson(li));
		// System.out.println( neighborhood );
	}
}